package de.bht.fb6.cg1.exercise1;

import java.awt.Color;

/**
 * CMY color space class.
 * 
 * @author Dominik Eckelmann
 */
public class CMY extends AbstractColorModel {

    /**
     * creates a new color in the CMY colorspace
     * @param c     initial {@link Color} to set.
     */
    public CMY(final Color c) {
        super(c);
    }

    /**
     * returns always 3.
     * 
     * single components are:
     * C - cyan
     * M - magenta
     * Y - yellow
     * @return component count.
     */
    @Override
    public int getComponentCount() {
        return 3;
    }

    /**
     * returns the range for every single component.
     * CMY here has a 0-255 range.
     * 
     * @return value range from the single values
     */
    @Override
    public Range[] getComponentRange() {
        final Range ranges[] = new Range[] {
                new Range(0, 255), new Range(0, 255), new Range(0, 255)
        };
        return ranges;
    }

    /**
     * returns the color space name
     * 
     * @return always CMY
     */
    @Override
    public String getName() {
        return "CMY";
    }

    /**
     * sets the object to a new color.
     * 
     * here the conversation from rgb to cmy
     * 
     * formula is partly taken from {@link http://www.easyrgb.com/index.php?X=MATH&H=11#text11}.
     * just with a expanded range from 0-255 insted of 0-1.
     * 
     * @param the new given color
     * @return the single color components in CMY
     */
    @Override
    public int[] setColor(final Color c) {
        modelColor[0] = 0 - c.getRed() + 255;
        modelColor[1] = 0 - c.getGreen() + 255;
        modelColor[2] = 0 - c.getBlue() + 255;

        color = c;
        return modelColor;
    }

    /**
     * calculates a given CMY value array to RGB color and sets the model to it.
     * 
     * formula is partly taken from {@link http://www.easyrgb.com/index.php?X=MATH&H=12#text12}.
     * just with a expanded range from 0-255 insted of 0-1.
     * 
     * @param color the new color to set.
     * @return new color as {@link Color} object.
     */
    @Override
    public Color setModelColor(final int[] color) {
        int r;
        int g;
        int b;

        if (color.length != getComponentCount()) {
            throw new IllegalArgumentException();
        }

        r = 0 - color[0] + 255;
        g = 0 - color[1] + 255;
        b = 0 - color[2] + 255;

        this.color = new Color(r, g, b);
        modelColor = color;
        return this.color;
    }

}
